﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using AjaxControlToolkit; 

namespace Commerce.CSK.Web.Controls
{
    public class ValidatedTextBox: TextBox , INamingContainer
    {
        [DefaultValue(false)]
        public bool Required { get; set; }

        [DefaultValue("A value is required")]
        public string RequiredErrorMessage { get; set; }

        
        public string ValidationExpression { get; set; }

        [DefaultValue("This value is not in the proper format.")]
        public string RegExErrorMessage { get; set; }

        public string CalloutCssClass { get; set; }

        public string CalloutHighlightCssClass { get; set; }

        public string CalloutCloseImageUrl { get; set; }

        public string   CalloutWarningIconImageUrl { get; set; }

        public string WatermarkText { get; set; }

        public string WatermarkCssClass { get; set; }

        protected override void CreateChildControls()
        {
            if (!this.DesignMode)
            {
                if (Required)
                {
                    //Add the required validator and callout. 
                    RequiredFieldValidator rqdValidator =
                        new RequiredFieldValidator();
                    rqdValidator.ControlToValidate = this.ID;
                    rqdValidator.ID = this.ID + "_Required"; 
                    rqdValidator.ErrorMessage = RequiredErrorMessage;
                    this.Page.Controls.Add(rqdValidator);
                    this.Page.Controls.Add(CreateCalloutExtender(rqdValidator)); 

                }
                if(!String.IsNullOrEmpty(this.ValidationExpression))
                {
                    RegularExpressionValidator regExValidator = 
                        new RegularExpressionValidator(); 
                    regExValidator.ControlToValidate = this.ID; 
                    regExValidator.ID = this.ID + "_RegEx"; 
                    regExValidator.ErrorMessage = this.RegExErrorMessage; 
                    regExValidator.ValidationExpression = this.ValidationExpression; 
                    this.Page.Controls.Add(regExValidator); 
                    this.Page.Controls.Add(CreateCalloutExtender(regExValidator)); 
                }
                if(!String.IsNullOrEmpty(this.WatermarkText))
                {
                    TextBoxWatermarkExtender waterMark = 
                        new TextBoxWatermarkExtender(); 
                    waterMark.WatermarkText = this.WatermarkText; 
                    waterMark.WatermarkCssClass = this.CssClass;
                    waterMark.TargetControlID = this.ID; 
                    this.Page.Controls.Add(waterMark);
                }
            }
            base.CreateChildControls();
        }

        private ValidatorCalloutExtender CreateCalloutExtender(Control validator)
        {
            ValidatorCalloutExtender callout =
                new ValidatorCalloutExtender();
            callout.ID = validator.ID + "Callout";
            callout.CloseImageUrl = this.CalloutCloseImageUrl;
            callout.CssClass = this.CalloutCssClass;
            callout.HighlightCssClass = this.CalloutHighlightCssClass;
            callout.CloseImageUrl = this.CalloutCloseImageUrl;
            callout.WarningIconImageUrl = this.CalloutWarningIconImageUrl;
            callout.TargetControlID = validator.ID;
            return callout; 

        }
    
    }
}
